﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using RBS.Common;
using RBS.Common.JsonEntity;
using Webdiyer.WebControls.Mvc;

namespace RBS.Web.Hotel.Controllers
{
    public class HotelRoleController : BaseController
    {
        //
        // GET: /HotelRole/ 
        //list
        public ActionResult List( int id = 1, string kword = null)
        {
            const int pageSize = 10;
            ChkAdminLevel("#", RBSEnums.ActionEnum.View.ToString()); //检查权限
            var bll = new RBS.Core.BLL.Hotel.H_Hotels_Role();
            // var admin = GetAdminInfo(); //取得当前管理员信息
            if (kword != null) kword = kword.Replace("'", "");
            var strTemp = new StringBuilder();
            if (!string.IsNullOrEmpty(kword))
            {
                strTemp.Append(" and (RoleName like  '%" + kword + "%' or RoleName like '%" + kword + "%')");
            }
            string strWhere = "and HotelId="+CurrentUser.HotelId+ strTemp.ToString();
            const string orderby = " id  desc";
            var startIndex = id;

            int totalItems = 0; //要分页的总记录数
            DataTable dt = bll.GetList(pageSize, startIndex, strWhere, orderby, out totalItems).Tables[0];
            //PagedList构造函数 
            var list = IListDataSet.DataTableToIList<RBS.Core.Model.Hotel.H_Hotels_Role>(dt);
            PagedList<RBS.Core.Model.Hotel.H_Hotels_Role> arts = null;
            if (list != null)
                arts = new PagedList<RBS.Core.Model.Hotel.H_Hotels_Role>(list, id, pageSize, totalItems);
            else
            {
                list = new List<RBS.Core.Model.Hotel.H_Hotels_Role>();
                arts = new PagedList<RBS.Core.Model.Hotel.H_Hotels_Role>(list, id, pageSize, totalItems);
            }
            return View(arts);
        } 
        //edit
        public ActionResult Edit(int id = 0)
        {
            var model = new RBS.Core.Model.Hotel.H_Hotels_Role
                {
                    HotelId = CurrentUser.HotelId
                };
            if (id != 0)
            { 
                var bll = new RBS.Core.BLL.Hotel.H_Hotels_Role();
                model = bll.GetModel(id);
            }
            ViewData.Model = model;
            return View();
        }
        [HttpPost]
        public ActionResult Edit(FormCollection collection)
        { 
            var a = collection;
            var model = new RBS.Core.Model.Hotel.H_Hotels_Role
            {
                HotelId = Convert.ToInt32(collection["HotelId"]),
                RoleName =  collection["RoleName"],
                Seq =  Convert.ToInt32( collection["Seq"]),
                
            }; 
            var bll = new RBS.Core.BLL.Hotel.H_Hotels_Role();
            if (collection["Id"] != "0")
            {
                model.Id = Convert.ToInt32(collection["Id"]);
                bll.Update(model); 
            }
            else
            { 
                bll.Add(model);
            }
            return RedirectToAction("List");
        } 
        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult Delete(string ids, string returnUrl)
        {
            var ret = new JsonReturnMessages();
            ChkAdminLevel("#", RBSEnums.ActionEnum.Delete.ToString()); //检查权限 
            try
            {
                foreach (var id in ids.TrimEnd(',').Split(',').ToList())
                {
                    new RBS.Core.BLL.Hotel.H_Hotels_Role().Delete(int.Parse(id));
                }
            }
            catch (Exception exception)
            {
                ret.IsSuccess = false;
                ret.Msg = "操作失败！";
                return Json(ret);
            } 
            ret.IsSuccess = true;
            ret.Msg = "操作成功";
            ret.Data = !string.IsNullOrEmpty(returnUrl) ? returnUrl : Url.Action("List");
            return Json(ret);
        } 
    }
}
